<template>
  <view v-if="!hasLogin" class="empty need_login">
    <u-empty text="未登录" mode="permission">
      <button size="mini" slot="bottom" class="go-to-login"
              @click="gotoPath('/pages/login/index?from=invoice' + serializeParam(options),false)">请先登录
      </button>
    </u-empty>
  </view>
  <view v-else class="container m-b6 p-bottom bs-address">
    <view class="common-header-xian"></view>
    <view class="addres-list" v-if="list.length>0">
      <view v-for="(item, index) in list" :key="index">
        <view class="address-header b-f m-top18">
          <view class="flow-header-left pl-12">
            <view class="flow-checkout-admin">
              <view class="item-list">
                名称： <text>{{ item.title }}</text>
                <u-tag v-if="item.type==0" type="primary" size="mini" text="普通增值税"></u-tag>
              <u-tag v-if="item.type==1" type="primary" size="mini" text="专用增值税"></u-tag>
              </view>

              <view class="item-list">
                税号： <text>{{ item.tax_number }}</text>
              </view>
            </view>

          </view>
        </view>

        <view class="address-list dis-flex b-f">
          <view class="list address-left">
            <radio-group @change="setDefaultData" class="radio-group">
              <label class="radio">
                <radio :checked="item.is_default==1" :disabled="item.is_default==1" color="#ff495e"
                       :value="item.invoice_id"></radio>
                <u-tag v-if="item.is_default" type="warning" size="mini" text="默认"></u-tag>
                <text class="cont" v-else>设为默认</text>
              </label>
            </radio-group>
          </view>
          <view class="list address-right dis-flex">
            <view @tap="editData" :data-id="item.invoice_id">
              <text class="iconfont icon-edit"></text>
              <text class="title">编辑</text>
            </view>
            <view @tap="removeData" :data-id="item.invoice_id">
              <text class="iconfont icon-lajixiang"></text>
              <text class="title">删除</text>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view v-else>
      <view class="bsshop-notcont">
        <text class="iconfont icon-wushuju"></text>
        <text class="cont">亲，您还没有发票抬头哦</text>
      </view>
    </view>
    <view class="footer-fixed f-32">
      <view @tap="createData" class="order-bt" style="width: 100%">添加新发票抬头</view>
    </view>
  </view>
</template>

<script>
import {list, remove, setDefault} from "@/common/invoice_api";

export default {
  data() {
    return {
      showChoose: false,
      list: [],
      options: null
    };
  },
  onLoad: function (options) {
    // 当前页面参数
    this.options = options;
    if (options.from) {
      this.showChoose = true
    }
  },
  onPullDownRefresh() {
    this.getList();
    setTimeout(() => {
      uni.stopPullDownRefresh()
    }, 2000)
  },

  onShow: function () {
    // 获取发票抬头列表
    this.getList();
  },
  methods: {
    copy(item){
      let str = item.province + ' ' + item.city + ' ' + item.area + ' ' + item.detail + ' ' + item.name + ' ' + item.phone
      this.copyText(str)
    },
    /**
     * 获取发票抬头列表
     */
    getList: function () {
      list().then(res => {
        this.list = res.data.list
      })
    },

    /**
     * 添加
     */
    createData: function () {
      uni.navigateTo({
        url: '/pages/packageA/invoice/form'
      });
    },

    /**
     * 编辑地址
     */
    editData: function (e) {
      uni.navigateTo({
        url: '/pages/packageA/invoice/form?id=' + e.currentTarget.dataset.id
      });
    },

    /**
     * 移除发票抬头
     */
    removeData: function (e) {
      let that = this;
      let id = e.currentTarget.dataset.id;
      uni.showModal({
        title: '操作警告',
        content: '您确定要移除当前发票抬头吗?',
        success: function (o) {
          if (o.confirm) {
            remove({
              invoice_id: id
            }).then(res => {
              that.$u.toast(res.msg)
              if (res.code === 0) {
                that.getList();
              }
            })
          }
        }
      });
    },


    setDefaultData: function (e) {
      let that = this;
      let id = e.detail.value;
      setDefault({invoice_id: id})
          .then(res => {
            that.$u.toast(res.msg)
            if (res.code === 0) {
              that.list = [];
              that.getList();
            }
          })

    }
  }
};
</script>
<style lang="scss" scoped>
@import 'index.scss';
</style>
